package com.sduept.nwld.dataserver.manager.faultforecast;

import com.sduept.bigdata.weather.entity.SimpleIcingModel;
import com.sduept.nwld.dataserver.model.faultforecast.IceFaultSample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IceFaultForcastManager {
	
	@Autowired
	private IceFaultForecastSampleManager sampleManager;
	
	public void setSamples(List<IceFaultSample> samples) {
		try {
			//这里将样本数据存库
			sampleManager.save(samples);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//TODO 其他的添加样本时需要的操作
		
	}
	
	public void train() {
		// TODO 对于样本的处理操作。可以设置一些参数。
		
	}
	
	/**
	 * 覆冰预测
	 * @param iceData
	 * @return result（key：类型，value：概率）
	 */
	public Map<String, Double> doForecast(SimpleIcingModel iceData) {
		
		List<IceFaultSample> sampleList = sampleManager.findAll();//样本数据
		List<double[]> faultList = new ArrayList<double[]>();//故障样本
		List<double[]> faultNotList = new ArrayList<double[]>();//非故障样本
		for(IceFaultSample iceFaultSample : sampleList){

			double[] sampleArr = new double[2];//样本数组，[时长，厚度]
			sampleArr[0] = iceFaultSample.getDuration();
			sampleArr[1] = iceFaultSample.getAveHeight();
			if(iceFaultSample.getFaultNum() > 0){//故障
				faultList.add(sampleArr);
			}else{//非故障
				faultNotList.add(sampleArr);
			}
		}
		Map<String, List<double[]>> sampleMap = new HashMap<String, List<double[]>>();
		sampleMap.put("fault", faultList);
		sampleMap.put("faultNot", faultNotList);
		double[] forcaseArr = new double[2];//顺序和样本数组一致
		forcaseArr[0] = iceData.getDuration();
		forcaseArr[1] = iceData.getAveHeight();
		Map<String, Double> result = NaiveBayes.faultForcast(sampleMap, sampleList.size(), forcaseArr);
		
		return result;
	}
	
	
}
